/*The MIT License

Copyright (c) <2008> <Samir Menon>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#ifndef NEURONTOOLS_H_
#define NEURONTOOLS_H_

#include <iostream>
#include <fstream>

namespace bis_neuron
{
	using namespace std;
	


	
/* =======================================================================
 *  CNeuronTools DECLARATION
 * =====================================================================*/
//	
//template <typename T> class CNeuronTools{
//public:
//	CNeuronTools<T>();
//	//~neurontools<T>(); TODO: Define later if needed
//};
//template <typename T> CNeuronTools<T>::CNeuronTools(){}


/* =======================================================================
 *  CThreadObject DECLARATION
 * =====================================================================*/

//template <typename T> class CThreadObject{
//	public:
//	CThreadObject<T>();
//};
//template <typename T> CThreadObject<T>::CThreadObject(){}



//Template classes below were written as part of cs227 at Stanford: Hylke Buisman and Samir Menon
template <class T> class CNode;
template <class T> class CDoublyLinkedList;

/* =======================================================================
 *  CNODE DECLARATION
 * =====================================================================*/

/* class declarations */
template <typename T> class CNode{
	private:
		CNode<T>* node_next;
		CNode<T>* node_prev;
		bool node_in_list;
		T node_value;
		bool list_mem_manage;
	public:
		CNode<T>();	CNode<T>(T value); CNode<T>* next(); CNode<T>* prev();
		void set_value(T val); bool in_list(); T get_value();
		void set_manual_mem_manage(bool manage) { list_mem_manage = !manage; };		
		int tag;		
		friend class CDoublyLinkedList<T>;
};

/* =======================================================================
 *  CNODE  IMPLEMENTATION
 * =====================================================================*/


template <typename T> CNode<T>::CNode(){
	this->node_next = NULL; this->node_prev = NULL; this->tag = -1;
	this->node_in_list = false;	this->list_mem_manage = false;
}

template <typename T> CNode<T>::CNode(T value){
	this->node_next = NULL; this->node_prev = NULL; this->node_in_list = false;
	this->node_value = value; this->tag = -1; this->list_mem_manage = false;
}

template <typename T> CNode<T>* CNode<T>::next() {	return this->node_next; }

template <typename T> CNode<T>* CNode<T>::prev() {	return this->node_prev; }

template <typename T> T CNode<T>::get_value(){
	return this->node_value;
}

template <typename T> void CNode<T>::set_value(T val){
	this->node_value = val;
}

template <typename T> bool CNode<T>::in_list() { return this->node_in_list; }

/* =======================================================================
 *  DOUBLY LINKED LIST DECLARATION
 * =====================================================================*/

template <class T> class CDoublyLinkedList{
	private:
		int list_size;
		CNode<T>* list_head;
		CNode<T>* list_tail;
		
	public:
		CDoublyLinkedList() {list_head = NULL; list_tail = NULL;list_size = 0;};
		
		int size();
		CNode<T>* head();
		CNode<T>* tail();
		
		void add(CNode<T>* new_item);
		CNode<T>* add(T new_item);
		void add_list(CDoublyLinkedList<T>* list);
		void add_after(CNode<T>* new_item, CNode<T>* after_this);
		void add_at_tail(CNode<T>* new_item);
		void add_at_head(CNode<T>* new_item);
		
		void push (CNode<T>* new_item) { add_at_head (new_item); };
		CNode<T>* top () { return list_head; };
		
		CNode<T>*  insert_sort(T item);
		void clear();
		void remove(CNode<T>* item);
		bool is_empty();
};

/* =======================================================================
 *  DOUBLY LINKED LIST IMPLEMENTATION
 * =====================================================================*/ 
//Wrapper Functions:
template <class T> CNode<T>* CDoublyLinkedList<T>::head() { return this->list_head; }
template <class T> CNode<T>* CDoublyLinkedList<T>::tail() { return this->list_tail; }
template <class T> int CDoublyLinkedList<T>::size(){	return this->list_size; }
//Operation Functions:
template <class T> CNode<T>* CDoublyLinkedList<T>::add(T new_item){
	CNode<T>* nn = new CNode<T>(new_item);
	nn->list_mem_manage = true;
	this->add_at_tail(nn);	
	return nn;
}
template <class T> bool CDoublyLinkedList<T>::is_empty(){
	if(this->list_head == NULL) {
		assert(this->list_tail == NULL);
		return true;
	}	
	return false;
}
// Adds another list's elements to this list
template <class T> void CDoublyLinkedList<T>::add_list(CDoublyLinkedList<T>* list){
	for (CNode<T>* walker = list->head(); walker != NULL; walker = walker->next()) {			
		this->add(walker->get_value());					
	}
}

// Insert sort // TODO: add comparator function, this is badly hardcoded
template <class T> CNode<T>* CDoublyLinkedList<T>::insert_sort(T item){
	CNode<T>* nn = new CNode<T>(item);
	nn->list_mem_manage = true;
	// Empty list case
	if (this->list_size == 0) {
		this->add(nn);
		return nn;
	}
	// Find insert position
	for (CNode<T>* walker = this->tail(); walker != NULL; walker = walker->prev()) {		
		if (walker->get_value() <= item) {			
			this->add_after(nn, walker);
			return nn;
		}
		
		if (walker->node_prev == NULL) {
			this->add_at_head(nn);
			return nn;
		}		
	}
	return NULL;
}

// Adds node as tail of list
template <class T> void CDoublyLinkedList<T>::add(CNode<T>* new_item){ add_at_tail (new_item); }
// Adds node as head of list
template <class T> void CDoublyLinkedList<T>::add_after(CNode<T>* new_item, CNode<T>* after_this){
	assert( new_item->node_next == NULL && new_item->node_prev == NULL );
	assert(!new_item->node_in_list);
	assert(after_this->node_in_list);
	
	if (after_this->node_next == NULL) {
		this->list_tail = new_item;
	} else {
		after_this->node_next->node_prev = new_item;
	}
	new_item->node_next = after_this->node_next;
	new_item->node_prev = after_this;	
	after_this->node_next = new_item;	
	this->list_size++;
	new_item->node_in_list = true;
}


// Adds node as head of list
template <class T> void CDoublyLinkedList<T>::add_at_tail(CNode<T>* new_item){
	assert( new_item->node_next == NULL && new_item->node_prev == NULL );
	assert(!new_item->node_in_list);
	// If list empty
	if (this->list_head == NULL && this->list_tail == NULL) {
		this->list_tail = new_item;
		this->list_head = new_item;		
		this->list_size++;
		new_item->node_in_list = true;
		return;
	}	
	new_item->node_next = NULL;
	new_item->node_prev = this->list_tail;	
	assert (new_item->node_prev != NULL);	
	new_item->node_prev->node_next = new_item;		
	this->list_tail = new_item;	
	this->list_size++;
	new_item->node_in_list = true;
}

// Adds node as head of list
template <class T> void CDoublyLinkedList<T>::add_at_head(CNode<T>* new_item){
	assert( new_item->node_next == NULL && new_item->node_prev == NULL );
	assert(!new_item->node_in_list);
	// If list empty
	if (this->list_head == NULL && this->list_tail == NULL) {
		this->list_tail = new_item;
		this->list_head = new_item;
		this->list_size++;
		new_item->node_in_list = true;
		return;
	}
	new_item->node_prev = NULL;
	new_item->node_next = this->list_head;	
	assert (new_item->node_next != NULL);
	new_item->node_next->node_prev = new_item;		
	this->list_head = new_item;
	this->list_size++;
	new_item->node_in_list = true;
}

template <class T> void CDoublyLinkedList<T>::clear(){
	CNode<T> *next, *walker = this->list_head;
	while (walker != NULL) {
		next = walker->node_next;
		walker->node_next = NULL;
		walker->node_prev = NULL;
		walker->node_in_list = false;
		if(walker->list_mem_manage) {
			delete walker;
		}
		walker = next;
	}
	this->list_head = NULL;
	this->list_tail = NULL;
	this->list_size = 0;
}

template <class T> void CDoublyLinkedList<T>::remove(CNode<T>* item){
	if(item == NULL) return;	
	if(!item->node_in_list) {
		return;
	}
	if (item->node_prev != NULL) { // Not at the beginning
		assert (item->node_prev->node_next == item);
		item->node_prev->node_next = item->node_next;
	} else { // Removing head of list
		// Update head pointer
		this->list_head = item->node_next;
	}
	if (item->node_next != NULL) { // Not at the end
		assert (item->node_next->node_prev == item);
		item->node_next->node_prev = item->node_prev;
	} else { // Removing tail of list
		// Update tail pointer
		this->list_tail = item->node_prev;
	}		
	item->node_prev = NULL;
	item->node_next = NULL;
	item->node_in_list = false;
	if(item->list_mem_manage) {
		delete item;
	}
	this->list_size--;
}
	

}//End of namespace neuron
#endif /*NEURONTOOLS_H_*/
